<?php

namespace app\admin\controller;

/**
 * 系统日志
 * User: tanhuaxin
 * Date: 2019/3/13
 * Time: 下午10:54
 */
class Log extends Controller
{

    /**
     * 日志列表
     */
    function lists()
    {
        if (request()->isAjax()) {
            $list['data'] = $this->checkdir_log(LOG_PATH);
            return ajax_list($list);
        } else {
            return $this->fetch();
        }
    }

    /**
     * 日志查看
     */
    function views()
    {
        $year = input('year');
        $value = input('value');
        $filename = LOG_PATH . $year . '/' . $value;
        $str = file_get_contents($filename); //将整个文件内容读入到一个字符串中
        $contents = str_replace("\r\n", "<br />", $str);
        return $this->fetch('views', compact('contents'));
    }


    /**
     * 检测文件夹
     */
    function checkdir_log($path, &$res = [])
    {
        if (is_dir($path)) {
            $dir = scandir($path);
            foreach ($dir as $value) {
                $sub_path = $path . '/' . $value;
                if ($value == '.' || $value == '..') {
                    continue;
                } else if (is_dir($sub_path)) {
                    $this->checkdir_log($sub_path, $res);
                } else {
                    $year = substr($path, -6);
                    $time = filemtime($sub_path);
                    $a['filemtime'] = date("Y-m-d H:i:s", $time);
                    $a['path'] = $sub_path;
                    $a['year'] = $year;
                    $a['value'] = $value;
                    $a['name'] = $year . $value;
                    $res[] = $a;
                }
            }
        }
        return array_reverse($res);
    }
}
